package com.example.leetdemo.plan1;

public class Sword022 {

    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        int x = 1;
        ListNode p = head.next, q= head.next.next;
        while (p != q) {
            if (q == null) {
                return null;
            }
            q = q.next.next;
            p = p.next;
            x++;
        }

        return null;
    }



    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
